package com.itcast.rediscache;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangtian105
 * @ClassName ThreadExecutorConfig.java
 * @projectName Redis
 * @Description 线程配置
 * @createTime 2020年11月19日 15:22:00
 */
@Configuration
public class ThreadExecutorConfig {
    @Bean
    public ScheduledThreadPoolExecutor getAsyncExecutor() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNamePrefix("redis_").build();
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(10, namedThreadFactory);
        // 设置最大线程数
        scheduledThreadPoolExecutor.setMaximumPoolSize(100);
        // 设置线程活跃时间（秒）
        scheduledThreadPoolExecutor.setKeepAliveTime(60, TimeUnit.SECONDS);
        scheduledThreadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());

        return scheduledThreadPoolExecutor;
    }
}
